package com.xihua.sx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xihua.sx.mapper.DriverMapper;
import com.xihua.sx.pojo.Driver;
import com.xihua.sx.pojo.PageData;
import com.xihua.sx.service.IDriverService;
import com.xihua.sx.utils.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2023-06-16
 */
@Service
public class DriverServiceImpl implements IDriverService {

    @Resource
    private DriverMapper driverMapper;


    /**
     * 分页查询
     * @param pageData
     * @return
     */
    @Override
    public ResponseMessage driverList(PageData pageData) {

        if (pageData.getPage()==null ||pageData.getSize()==null){
            pageData.setPage(1);
            pageData.setSize(5);
        }
        //通过前端传过来的pageData对象中的数据，创建分页对象page
        Page<Driver> page = new Page<>(pageData.getPage(), pageData.getSize());

        //执行查询
        Page<Driver> driverPage = driverMapper.driverList(page);

        return new ResponseMessage(200,"driver list",driverPage);
    }

    @Override
    public ResponseMessage driverOne(Integer id) {
        QueryWrapper<Driver> questionQueryWrapper = new QueryWrapper<>();
        questionQueryWrapper.eq("driver_id",id);
        Driver driver = driverMapper.selectOne(questionQueryWrapper);

        return new ResponseMessage(200,"driverOne",driver);

    }

    /**
     * 新增
     * @param driver
     * @return
     */
    @Override
    public ResponseMessage insertDriver(Driver driver) {
        int insert = driverMapper.insert(driver);

        return new ResponseMessage(200,"insert driver",insert);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public ResponseMessage deleteDriver(Integer id) {
        int i = driverMapper.deleteById(id);

        return new ResponseMessage(200,"deleteById",i);
    }

    /**
     * 更新
     * @param driver
     * @return
     */
    @Override
    public ResponseMessage updateDriver(Driver driver) {
        int i = driverMapper.updateById(driver);

        return new ResponseMessage(200,"updateById",i);
    }

    /**
     * 对性别，名字，地址，驾照类型进行模糊查询
     * @param driver
     * @return
     */
    @Override
    public ResponseMessage selectLike(Driver driver) {
        QueryWrapper<Driver> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(driver.getDriverSex()!=null,"driver_sex",driver.getDriverSex());
        queryWrapper.like(driver.getDriverName()!=null,"driver_name",driver.getDriverName());
        queryWrapper.like(driver.getAddress()!=null,"address",driver.getAddress());
        queryWrapper.like(driver.getLicenseType()!=null,"license_type",driver.getLicenseType());

        List<Driver> driverList = driverMapper.selectList(queryWrapper);

//        lambda表达式进行条件组合查询
//        String name=driver.getDriverName();
//        System.out.println("名字、、、、、、"+name);
//        queryWrapper.and(
//                wrapper ->
//                        wrapper.like("driver_name",driver.getDriverName() ).or().like("address", driver.getAddress())
//        );
//
//        List<Driver> driverList = driverMapper.selectList(queryWrapper);


//
        return new ResponseMessage(200,"selectLike",driverList);

//        /*
//        * // QueryWrapper<T>：mybatis-plus 用来封装 sql 语句条件的对象
//        QueryWrapper<TaskQuestion> queryWrapper = new QueryWrapper<>();
//        /*
//        * eq()：等于
//        * like()：like，模糊查询
//        * 参数：
//        *   第一个：条件表达式，实现动态 sql 的
//        *   第二个：数据库表字段名
//        *   第三个：数据库表字段名对应的值
//        * 动态 SQL：当你满足某个条件，我就把这个语句加入到 SQL 的执行中
//        *   查询单个用户、查询用户列表
//        *   select * from user
//        *   select * from user where username=xxx
//        * */
//        // 设置 courseId
//        queryWrapper.eq("course_id",courseId); // course_id = courseId
//        // 设置 order by rand()
//        queryWrapper.orderBy(true,false,"rand()");
//        // 设置 limit 5
//        queryWrapper.last("limit 5");
//        // 执行 sql
//        /*
//         * selectList()：查询列表
//         * selectOne()：单个查询
//         * selectPage()：分页查询
//         * delete()：删除
//         * insert()：新增
//         * update()：修改
//         * */
//        List<TaskQuestion> taskQuestionList = taskQuestionMapper.selectList(queryWrapper);
//        return new ResponseMessage(200,"task question list",taskQuestionList);
//        * */
    }
}
